<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *种植管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('confirm','cancel','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
}
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'种植列表'),
);
$return['status']=array('0'=>'待处理','1'=>'播种中','2'=>'已收获');
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','land_id','seed_id','mid','plantarea','order_id','stage','worm','weed','water','shifei','status','createtime');
	$list=pdo_getslice('jy_farmer_plant',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$land=pdo_get('jy_farmer_land',array('id'=>$item['land_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['lclassify_title']=pdo_getcolumn('jy_farmer_lclassify',array('id'=>$land['classify_id'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['land_title']=$land['title'];
			$item['member']=pdo_get('core_members',array('id'=>$item['mid']));
			$order=pdo_get('jy_farmer_order',array('id'=>$item['order_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['username']=$order['username'];
			$item['mobile']=$order['mobile'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'客户购买土地后，需购买相应的种子，这里才能进入种植流程！','edit'=>false,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'member','title'=>'用户信息','type'=>'member'),
			array('type'=>'img_string','title'=>'联系人/电话',
			'data'=>array(
				array('title'=>'','name'=>'username'),
				array('title'=>'','name'=>'mobile')
				)
			),
			array('name'=>'lclassify_title','title'=>'地块名称','type'=>'string'),
			array('name'=>'land_title','title'=>'土地名称','type'=>'string'),
			array('name'=>'plantarea','title'=>'面积','type'=>'unitstring','unit'=>'㎡'),
			array('name'=>'createtime','title'=>'种植时间','type'=>'time'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),'action'=>array(
			array('op'=>'seed','title'=>'种植详情','type'=>'href','show'=>array('status'=>1)),
		)),
		'search'=>array(
			array('name'=>'username','width'=>15,'title'=>'用户姓名','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='seed'){
	$plant_id=$_RQ['id'];
	if(!$plant_id){
		exi('请选择订单！','error');
	}
	$order=pdo_get('jy_farmer_plant',array('id'=>$plant_id));
	$return['params']['id']=$plant_id;
	$params=array('plant_id'=>$plant_id,'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['status'])){
		$params['status']=$_RQ['status'];
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_farmer_plant_log',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$seed_id=pdo_getcolumn('jy_farmer_myseed',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['myseed_id']),'seed_id');
			$seed=pdo_get('jy_farmer_seed',array('id'=>$seed_id,'uniacid'=>$_SESSION['uniacid']));
			$item['seed_title']=$seed['title'];
			$item['land_title']=$land['title'];
			$item['member']=pdo_get('core_members',array('id'=>$item['mid']));
			$order_id=pdo_getcolumn('jy_farmer_myseed',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['myseed_id']),'order_id');
			$order=pdo_get('jy_farmer_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
			$item['sendtype']=$order['sendtype'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'客户购买土地后，需购买相应的种子，客户前端选择要种植的土地，然后进入种植流程！','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'用户信息','type'=>'member'),
			array('name'=>'seed_title','title'=>'种子名称','type'=>'string'),
			array('name'=>'plantarea','title'=>'种植面积','type'=>'unitstring','unit'=>'㎡'),
			array('name'=>'shoukg','title'=>'实际收获','type'=>'unitstring','unit'=>'KG'),
			array('name'=>'sendtype','title'=>'配送方式','type'=>'radio','options'=>'send_types'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),'action'=>array(
			array('type'=>'modal','op'=>'fa','title'=>'发货','show'=>array('sendtype'=>1,'status'=>array(4,5))),
			array('type'=>'modal','op'=>'hexiao','title'=>'核销','show'=>array('sendtype'=>0,'status'=>array(4,5))),
		)),
		'search'=>array(
			array('name'=>'status','width'=>10,'title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['status']=$this->gdopt_status;
	$return['send_types']=$this->send_types;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='fa'){
	if($_RQ['postdata']){
		$post=get_postdata();
		pdo_update('jy_farmer_plant_log',array('status'=>7,'express'=>$post['express'],'expresssn'=>$post['expresssn']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['id']));
		exi('快递发货完成！','','list');
	}
	$item=pdo_get('jy_farmer_plant_log',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定订单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'快递发货',
		'edit'=>array(
			array('name'=>'shoukg','title'=>'发货收获','type'=>'unitstring','unit'=>'KG','read'=>true),
			array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name'),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn'),
		),
	);
	$return['express']=model('jy_weishop/model')->express_types();
	exi($return);
}
if($_RQ['op']=='hexiao'){
	if($_RQ['postdata']){
		$post=get_postdata();
		pdo_update('jy_farmer_plant_log',array('status'=>6),array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['id']));
		exi('核销收获完成！','','list');
	}
	$item=pdo_get('jy_farmer_plant_log',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定订单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'客户上门自提',
		'edit'=>array(
			array('name'=>'shoukg','title'=>'核销收获','type'=>'unitstring','unit'=>'KG','read'=>true),
		),
	);
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_farmer_plant',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_farmer_plant',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
